Macromedia Flex Article

A Step-by-Step Guide to Building an Application with Flex


Table of Contents

  1. Introduction
  2. Flex Store
  3. Code Walkthrough
  4. MXML Components
  5. ActionScript Components
  6. Writing Code

Writing Code

Although Flex makes it very easy to structure an enterprise application using best practices design, it is also a very easy to create compelling experiences in very few lines of code. In this section, you'll recreate some of the functionality you saw in the Flex Store application. Specifically, you will call a web service, create model to hold your data, populate the model with a web service, invoke a method to get data, create the event to trigger the method, and bind the incoming data to a user interface control. Note that this section only applies to you if you have installed Flex on your server.

  1. Type the following code in your favorite text editor or IDE:

    <?xml version="1.0" encoding="utf-8"?>
    
    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" height="600">
    

    The first thing you do is set up your application by setting the XML version and the application tag. The xmlns property in the tag specifies the XML namespace. Flex examples use the mx namespace for readability but it is optional. You can use the default namespace by not specifying a prefix and you can specify additional namespaces for references to custom tags.

  2. Call a web service to get your data. Copy the following code:

    <mx:WebService id="srv" wsdl="@ContextRoot()/services/CatalogWS?wsdl">
    <mx:operation name="getList"/>
    </mx:WebService>
    
  3. Create the model to hold your data and populate it with the web service. Copy the following code:   

    <mx:Model id="catalog">
    {srv.getList.result}
    </mx:Model>
    
  4. You must add a button to trigger the event that gets the data. Copy the following code: 

    <mx:Button label="Get Data" click="srv.getList()"/>
    
  5. To display your data and close the application, use the grid view by copying the following into your code:

    <mx:DataGrid dataProvider="{catalog}">
    </mx:DataGrid>
    
    </Application>
    
  6. Save your code to flex default install\samples\flexstore\demo.mxml and browse to http://flex default install/samples/flexstore/demo.mxml

    With those few lines of code, you can access and display data from a web service. By default, the data grid displays all of the data returned from the web service but this isn't what your user wants to view.

  7. To format the data grid to show only the product name and price, add the following code between the dataProvider="{catalog}" statement and the </DataGrid> tag:

    <mx:columns>
    	<mx:Array>
    	<mx:DataGridColumn columnName="name" headerText="Name" width="200"/>
    	<mx:DataGridColumn columnName="price" headerText="Price" width="100"/>
    	</mx:Array>
    </mx:columns>
    
  8. Save your file and view it in the browser.

    You can also display the same data in a different view, in this case as a grid of thumbnail images.

  9. To do this, enter the following after the data grid block and before the close application tag:

    <mx:Tile label="Thumbnails">
    <mx:Repeater id="list" dataProvider="{catalog}">
    <mx:VBox>
    <mx:Image source="{list.currentItem.image}" width="75" height="70"/>
    <mx:Label text="{list.currentItem.name}"/>
    </mx:VBox>
    </mx:Repeater>
    </mx:Tile>
    

    The Repeater object lets you dynamically repeat any number of controls or containers specified in MXML tags, at runtime. Flex repeats the control or container based on an array of dynamic data, such as an Array object returned from a web service.

    You use the tile container to display the items that you iterate over in this example. The tile container is a layout manager that lays out its children in one or more vertical columns or horizontal rows, starting new rows or columns as necessary.

  10. Save your file and view it in the browser.

    While it is nice to see your data in two different views, it isn't a particularly well organized. With Flex, you can easily organize the data on the screen by supplying a number of navigators. For this example, you'll organize the two views into a tab navigator.

  11. Surround the data grid block with the Canvas tag:

    <mx:Canvas label=”Grid”>
    

    Data grid block

    </mx:Canvas>

    A navigator contains other containers, so you first wrap the data grid in a container tag. The label is what will display in the tab of the tab navigator.

  12. Add a label to the tile container by entering your label inside of the <mx:Tile> tag so that it reads:

    <mx:Tile label=”Thumbnails”>
    
  13. Surround the <mx:Canvas> and </mx:Tile> tags with the opening and closing tab navigator tags:

    <mx:TabNavigator>
    <mx: Canvas label=”Grid”>
    		.
    		.
    		.
    </mx:Tile>
    </mx:TabNavigator>
    
  14. Save your file and view it in the browser. The views now display in two tabs.

    The final file should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" height="600">
    <mx:WebService id="srv" wsdl="@ContextRoot()/services/CatalogWS?wsdl">
    <mx:operation name="getList"/>
    </mx:WebService>
    <mx:Model id="catalog">{srv.getList.result }</mx:Model>
    <mx:Button label="Get Data" click="srv.getList()"/>
    
    <mx:TabNavigator>
    <mx:Canvas label="Grid">
    <mx:DataGrid dataProvider="{catalog}">
    <mx:columns>
    <mx:Array>
    <mx:DataGridColumn columnName="name" headerText="Name" width="200"/>
    <mx:DataGridColumn columnName="price" headerText="Price" width="100"/>
    </mx:Array>
    </mx:columns>
    </mx:DataGrid>
        
        </mx:Canvas>
        
        <mx:Tile label="Thumbnails">
        <mx:Repeater id="list" dataProvider="{catalog}">
        <mx:VBox>
        <mx:Image source="{list.currentItem.image}" width="75" height="70"/>
        <mx:Label text="{list.currentItem.name}"/>
        </mx:VBox>
        </mx:Repeater>
        </mx:Tile>
        
        </mx:TabNavigator>
        
        </mx:Application>
    

That's an amazing amount of functionality in only 26 lines of code and that's just the beginning. With over 45 components and controls available, developers can quickly and effectively create great user interfaces that offer users a new level of responsiveness and usability and in turn, offer businesses a more effective way to do business on the web.

While the Flex Store example is a simple one, it points out three important principles:

  • Experience matters
  • Good user experience doesn't mean sacrificing good architecture
  • Good user experiences come from good design

Flex Store shows that even in a simple design, the client side data model and Flex's rich user interface controls and behaviors allows developers to create responsive, intuitive user interfaces. The architecture of Flex Store represents how Flex Store integrates with best practices enterprise architectures and allows teams of developers to participate. And finally, Flex's default HALO design, as well as the ease with which designer and developers work together, makes it easy for Flex applications to embody the best practices design that makes for great user experiences. Read more about HALO in Understanding the Macromedia Flex Experience Model, by Mike Sundermeyer.

For more examples of Flex code, see the Flex Samples Explorer in http://flex default install/samples/explorer/explorer.mxml if you've installed Flex. You will find a number of small code samples that show how to access data, set security, apply behaviors, and many other tasks. If you haven't installed Flex, check out the Flex Application Examples. Check out the Flex Developer Center Getting Started area get more familiar with Flex.